package com.woniu.immunologicalmechanism.service.impl;

import com.woniu.common.entity.Organization;
import com.woniu.common.entity.ResponseResult;
import com.woniu.immunologicalmechanism.mapper.OrganizationMapper;
import com.woniu.immunologicalmechanism.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrganizationServiceImpl implements OrganizationService {
  @Autowired
   OrganizationMapper organizationMapper;

    //添加机构
    @Override
    public ResponseResult addorganization(Organization organization) {
     ResponseResult result=new ResponseResult();

        if(organization==null){
           return ResultUtil.empty(result);
        }
       organization.setStatus(1);
      try {
          organizationMapper.insertorganization(organization);
          ResultUtil.selectOk(result);
      }
      catch (Exception e){
          e.printStackTrace();
         ResultUtil.error(result);
      }
        return result;
    }

    //修改机构信息
    @Override
    public ResponseResult updateorganization(Organization organization) {
        ResponseResult result=new ResponseResult();

        try {
            organizationMapper.updateorganization(organization);
           ResultUtil.updateOk(result);
        }
        catch (Exception e){
            e.printStackTrace();
            ResultUtil.error(result);
        }
        return result;
    }

    //删除免疫机构
    @Override
    public ResponseResult deleteorganization(int oid) {
        ResponseResult result=new ResponseResult();


        try {
            organizationMapper.deleteorganization(oid);
           ResultUtil.deleteOk(result);
        }
        catch (Exception e){
            e.printStackTrace();
            ResultUtil.error(result);
        }
        return result;
    }
    //根据id查询免疫机构信息
    @Override
    public ResponseResult<Organization> selectOrganization(int oid) {
        ResponseResult result=new ResponseResult();


        try {
            Organization organization = organizationMapper.selectOrganization(oid);
            result.setData(organization);
           ResultUtil.selectOk(result);
        }
        catch (Exception e){
            e.printStackTrace();
            ResultUtil.error(result);
        }
        return result;
    }
    //根据名字模糊查询所有免疫机构信息
    @Override
    public ResponseResult<List<Organization>> selectOrganizationByname(String oname) {
        ResponseResult result=new ResponseResult();


        try {
            List<Organization> organizationList = organizationMapper.selectOrganizationByname(oname);
            result.setData(organizationList);
            ResultUtil.selectOk(result);
        }
        catch (Exception e){
            e.printStackTrace();
            ResultUtil.error(result);
        }
        return result;
    }

    //根据uid查询免疫机构信息
    @Override
    public ResponseResult<List<Organization>> getOrganizationByuid(int uid) {
        ResponseResult result=new ResponseResult();


        try {
            int oid = organizationMapper.selectOrganizationId(uid);
            Organization organization = organizationMapper.selectOrganization(oid);
            result.setData(organization);
            ResultUtil.selectOk(result);
        }
        catch (Exception e){
            e.printStackTrace();
            ResultUtil.error(result);
        }
        return result;
    }



}
